from django.core.mail import send_mail
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect

from forms import ContactForm

def thanks(request):
    return render_to_response('contact_thanks.html')

def contact_orig(request):
    errors = []
    if request.method == "POST":
        contactForm = ContactForm(request.POST)
        if contactForm.is_valid():
            cd = contactForm.cleaned_data
            num_sent = send_mail(
                cd['subject'],
                cd['message'],
                cd.get('email', 'stthomases@gmail.com'), ['spam-me-2000@hotmail.com'],
            )
            errors.append("Msg sent: %d" % num_sent)
            return HttpResponseRedirect('/contact/thanks/')
        else:
            errors.append("contactForm NOT valid!")
            print("contact form not valid!!!")
    else:
        contactForm = ContactForm(initial={'subject':'I love your site?!'})
        print("contact-form [is_bound:%s]" % [str(contactForm.is_bound)])
    return render_to_response('contact_form.html', {'contact_form': contactForm, "errors":errors})


def contact(request):
    errors = []
    if request.method == "POST":
        contactForm = ContactForm(request.POST)
        if contactForm.is_valid():
            cd = contactForm.cleaned_data
            num_sent = send_mail(
                cd['subject'],
                cd['message'],
                cd.get('email', 'stthomases@gmail.com'), ['spam-me-2000@hotmail.com'],
            )
            errors.append("Msg sent: %d" % num_sent)
            return HttpResponseRedirect('/contact/thanks/')
        else:
            errors.append("contactForm NOT valid!")
            print("contact form not valid!!!")
    else:
        contactForm = ContactForm(initial={'subject':'I love your site?!'})
        print("contact-form [is_bound:%s]" % [str(contactForm.is_bound)])
    return render_to_response('contact_form_v2.html', {'contact_form': contactForm, "errors":errors})

